<?php
namespace app\index\controller;

use app\index\Controller;


class Index extends Base
{
    //用户取杆即开锁操作
    public function unlock()
    {   
        $Lock   = model('Lock');//实例化锁模型
        $Charge = model('Charge');//实例化消费记录模型
        
        //获取用户扫描二维码的标号
        $number = input('ball_arm_number');
        //根据球杆编号查询该球杆的状态信息
        $lock_info = $Lock->getLockInfo($number);
        
        $data['status'] = 0;
        
        //用户取球杆的业务处理
        if($lock_info['status'] == 0){
            //通知远程柜子开锁
            if($this->request($lock_info['box_number'],$lock_info['lock_number'],1)){
                //修改杆的状态为使用中
                $data['status'] = $Lock->updateLockUsingStatus($lock_info['id'],$this->user_id);
                //插入计费信息
                $data['charge_id'] = $Charge->StartBilling($this->user_id,$lock_info['id']);//订单的id
            }else{
                $data['status'] = "通讯失败！";
            }
        }else{
            //该球杆正在被用户使用，提示用户使用其它球杆
            $data['info']   = getBallArmStatusInfo($lock_info['status']);
        }

        return json($data);
    }

    //用户还杆操作
    public function returnBallArm(){
        $Lock   = model('Lock');//实例化锁模型
       
        
        $number = input('ball_arm_number');

        $data   = $Lock->userReturnBallArm($number,$this->user_id);
        
        if($data['status'] == 1){
            $lock_info = $Lock->getLockInfo($number);
            //通知远程柜子关锁
            $this->request($lock_info['box_number'],$lock_info['lock_number'],0);
        }
       
        return json($data);
    }

    //计算用户的消费
    public function charge(){
        $form_id   = input('form_id');
        $Charge    = model('Charge');
        $user_feedback = input('user_feedback');
        $data =  $Charge->endBilling($this->user_id,$user_feedback);
        //发送微信小程序消息提示
        if($data['status'] == 1){
            $this->sendMessageInfo($form_id,$data['order_info']);
        }
        return json($data);
    }
    
    //发送微信小程序通知
    private function sendMessageInfo($form_id,$order_info){
        $return_time = date('Y-m-d H:i',$order_info['end_time']);
        $duration    = formatDuration($order_info['duration']);
        $into        = "恭喜您用杆成功，本次的消费金额：{$order_info['amount']}元";
        //获取用户的open_id
        $openid = db('user')->where('id',$this->user_id)->value('openid');
        $message_info['touser']           = $openid;
        $message_info['template_id']      = 'j-4aHxsLUQV9MPrQnS0gooE00zrmQ11egRzbJElpCIE';
        $message_info['page']             = 'usagelog';
        $message_info['form_id']          = $form_id;

       // $message_info['data']['keyword1'] = array('value'=>$order_info['order_number'],'color'=>"#173177");
        $message_info['data']['keyword1'] = array('value'=>$return_time,'color'=>"#173177");
        $message_info['data']['keyword2'] = array('value'=>$duration,'color'=>"#173177");
        $message_info['data']['keyword3'] = array('value'=>$into ,'color'=>"#173177");
       
        $access_token = $this->getAccessToken();
        $url    = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";
       
        $result = httpRequest($url,'POST',json_encode($message_info));
        
        file_put_contents('test.text', $form_id);
    }

    //开锁的接口
    private function request($equipment,$code,$on){
        $data['equipment'] = $equipment;
        $data['code']      = $code;
        $data['on']        = $on;
 
        $url = "http://47.93.103.53:9001/WebService1.asmx/MQTTService";
        $result = httpRequest($url,'POST',$data);
        return $result;
    }

    //用户的消费使用记录
    public function usagelog(){
        $Charge        = model('charge');
        $user_charge = $Charge->getUserCharge($this->user_id);

        foreach ($user_charge as $key => $value) {
            $user_charge_list[$key][] = date('Y-m-d H:i',$value['end_time']);
            $user_charge_list[$key][] = '野豹-魔法';
            $user_charge_list[$key][] = formatDuration($value['duration']);
            $user_charge_list[$key][] = $value['club_name'];
            $user_charge_list[$key][] = $value['amount'];
        }
        return json($user_charge_list);
    }

    //用户的红包
    public function userRedpacket(){
        $Redpacket = model('redpacket');
        //用户红包list
        $data['user_redpacket_list'] = $Redpacket->getUserRedpacketList($this->user_id);
        foreach ($data['user_redpacket_list'] as $key => $val) {
            $data['user_redpacket_list'][$key]['use_time'] = $data['user_redpacket_list'][$key]['use_time'] == 0 ? 0 : date('Y-m-d H:i',$data['user_redpacket_list'][$key]['use_time']);
        }
        //用户未使用红包数量
        $data['not_used_count']      = $Redpacket->getUserRedpacketCount($this->user_id);

        return json($data);
    }

    //用户使用红包
    public function useRedPacket(){
        $Redpacket = model('redpacket');
        $Recharge  = model('recharge');

        $id = input('id');
        
        $data['status'] = 0;
        
        //使用红包
        $use_result = $Redpacket->userUseRedPacket($id);
        if($use_result >=0){
            //账户充值
            if($money = $Recharge->userRecharge($this->user_id,$use_result)){
                //添加充值记录
                if($Recharge->addRechargeLog($this->user_id,$use_result,2)){
                    $data['money']  = $money;
                    $data['status'] = 1;
                }

            }
            
        }
        return json($data);
    }

    //用户充值
    public function recharge(){
        $Recharge  = model('recharge');

        $money = input('recharge_money')*100;
        
        $data['result'] = $Recharge->pay_place_order($this->user_id,$money);     
        
        return json($data);
    }

    //获取用的的money
    public function getMoney(){
        return db('user')->where('id',$this->user_id)->value('money');
    }

    //用户反馈
    public function addUserFeedback(){
        $Lock = model('Lock');
        $ball_arm_number = input('ball_arm_number');
        if($ball_arm_number){
           //根据球杆编号查询该球杆的状态信息
            $lock_info = $Lock->getLockInfo($ball_arm_number);
            $insert_info['lock_id'] = $lock_info['id'];
        }
        $insert_info['user_id']          = $this->user_id;
        $insert_info['feedback_type']    = input('feedback_type');
        $insert_info['feedback_content'] = input('feedback_content');
        $insert_info['time']             = time();


        $data['status'] = 0;
        if(db('user_feedback')->insert($insert_info)){
            $data['status'] = 1;
        }
        return json($data);

     }
}
